package com.hui;

/**
 * Jackson ObjectMapper类
 */
//ObjectMapper类是Jackson库的主要类。它提供一些功能将转换成Java对象匹配JSON结构，反之亦然。它使用JsonParser和JsonGenerator的实例实现JSON实际的读/写。
//以下是org.codehaus.jackson.map.ObjectMapper类的声明：
// 演示实例：

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.subject.SimplePrincipalCollection;

import java.io.IOException;

/**
 * 这里注意，笔者选择的这个序列化工具，官方提供了多种序列化方式，最好不要使用官方提供带Jackson标识的，会出现shiro session存入redis中，但从redis中取出数据时反序列化后session对象中数据丢失，原因未查证。
 */
public class JacksonTester {
    public static void main(String args[]) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        String jsonString = "[\"org.apache.shiro.session.mgt.SimpleSession\",{\"id\":\"1\",\"startTimestamp\":[\"java.util.Date\",1511597363131],\"stopTimestamp\":null,\"lastAccessTime\":[\"java.util.Date\",1511597365859],\"timeout\":1800000,\"expired\":false,\"host\":\"0:0:0:0:0:0:0:1\",\"attributes\":[\"java.util.HashMap\",{\"org.apache.shiro.subject.support.DefaultSubjectContext_AUTHENTICATED_SESSION_KEY\":true,\"org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY\":[\"org.apache.shiro.subject.SimplePrincipalCollection\",{\"realmPrincipals\":[\"java.util.LinkedHashMap\",{\"com.hui.auth.config.shiro.AuthRealm\":[\"java.util.LinkedHashSet\",[\"admin\"]]}],\"empty\":false,\"realmNames\":[\"java.util.LinkedHashMap$LinkedKeySet\",[\"com.hui.auth.config.shiro.AuthRealm\"]],\"primaryPrincipal\":\"admin\"}],\"redisUser\":[\"com.hui.auth.entity.ExcelUser\",{\"id\":1,\"userName\":\"admin\",\"password\":\"123\",\"createUser\":\"ssww\",\"createTime\":\"2017-10-27 11:10:19.0\",\"updateUser\":\"ssww\",\"updateTime\":\"2017-09-23 13:53:43.0\",\"lastLogin\":\"2017-11-25 15:31:13.0\",\"status\":0,\"roles\":[\"java.util.ArrayList\",[[\"com.hui.auth.entity.AuthRole\",{\"id\":1,\"name\":\"张哥\",\"menu\":null,\"permission\":null}]]],\"permissions\":[\"java.util.ArrayList\",[[\"com.hui.auth.entity.AuthPermission\",{\"id\":1,\"url\":\"/auth/user/insert\",\"name\":\"张哥\",\"mid\":5}],[\"com.hui.auth.entity.AuthPermission\",{\"id\":2,\"url\":\"/auth/user/select_all\",\"name\":\"gg\",\"mid\":5}],[\"com.hui.auth.entity.AuthPermission\",{\"id\":3,\"url\":\"/auth/user/delete\",\"name\":\"张哥\",\"mid\":5}],[\"com.hui.auth.entity.AuthPermission\",{\"id\":4,\"url\":\"/auth/user/update\",\"name\":\"gg\",\"mid\":5}],[\"com.hui.auth.entity.AuthPermission\",{\"id\":5,\"url\":\"/auth/user/select\",\"name\":\"张哥\",\"mid\":5}]]],\"roleIds\":\"1\"}]}],\"valid\":true,\"stopped\":false,\"attributeKeys\":[\"java.util.HashMap$KeySet\",[\"org.apache.shiro.subject.support.DefaultSubjectContext_AUTHENTICATED_SESSION_KEY\",\"org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY\",\"redisUser\"]],\"timedOut\":false,\"attributesLazy\":[\"java.util.HashMap\",{\"org.apache.shiro.subject.support.DefaultSubjectContext_AUTHENTICATED_SESSION_KEY\":true,\"org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY\":[\"org.apache.shiro.subject.SimplePrincipalCollection\",{\"realmPrincipals\":[\"java.util.LinkedHashMap\",{\"com.hui.auth.config.shiro.AuthRealm\":[\"java.util.LinkedHashSet\",[\"admin\"]]}],\"empty\":false,\"realmNames\":[\"java.util.LinkedHashMap$LinkedKeySet\",[\"com.hui.auth.config.shiro.AuthRealm\"]],\"primaryPrincipal\":\"admin\"}],\"redisUser\":[\"com.hui.auth.entity.ExcelUser\",{\"id\":1,\"userName\":\"admin\",\"password\":\"e10adc3949ba59abbe56e057f20f883e\",\"createUser\":\"张哥\",\"createTime\":\"2017-10-27 11:10:19.0\",\"updateUser\":\"张哥\",\"updateTime\":\"2017-09-23 13:53:43.0\",\"lastLogin\":\"2017-11-25 15:31:13.0\",\"status\":0,\"roles\":[\"java.util.ArrayList\",[[\"com.hui.auth.entity.AuthRole\",{\"id\":1,\"name\":\"张哥\",\"menu\":null,\"permission\":null}]]],\"permissions\":[\"java.util.ArrayList\",[[\"com.hui.auth.entity.AuthPermission\",{\"id\":1,\"url\":\"/auth/user/insert\",\"name\":\"张哥\",\"mid\":5}],[\"com.hui.auth.entity.AuthPermission\",{\"id\":2,\"url\":\"/auth/user/select_all\",\"name\":\"ss\",\"mid\":5}],[\"com.hui.auth.entity.AuthPermission\",{\"id\":3,\"url\":\"/auth/user/delete\",\"name\":\"张哥\",\"mid\":5}],[\"com.hui.auth.entity.AuthPermission\",{\"id\":4,\"url\":\"/auth/user/update\",\"name\":\"ss\",\"mid\":5}],[\"com.hui.auth.entity.AuthPermission\",{\"id\":5,\"url\":\"/auth/user/select\",\"name\":\"张哥\",\"mid\":5}]]],\"roleIds\":\"1\"}]}],\"alteredFieldsBitMask\":219}]";

        //map json to student
        try {
//            SimpleSession collection2 = mapper.readValue(jsonString, SimpleSession.class);
//            System.out.println(collection2);
//            System.out.println(jsonString);

            SimpleSession session = new SimpleSession();
            String sessionString = mapper.writeValueAsString(session);
            System.out.println(sessionString);
            SimpleSession session1 = mapper.readValue(sessionString, SimpleSession.class);
            System.out.println(session1);

            SimplePrincipalCollection collection = new SimplePrincipalCollection();
            String collString = mapper.writeValueAsString(collection);
            System.out.println(collString);
            SimplePrincipalCollection collection1 = mapper.readValue(collString, SimplePrincipalCollection.class);
            System.out.println(collection1);

            Student s = new Student();
            s.setAge(20);
            s.setName("小张");
            String cString = mapper.writeValueAsString(s);//只要有get方法，他就会写属性

            System.out.println(cString);
            Student student = mapper.readValue(cString, Student.class);
            System.out.println(student);

        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class Student {
    private final String testFinal = "测试final";
    private String name;
    private int age;

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getHaxi() {
        return "ss";
    }


    public String getTestFinal() {
        return testFinal;
    }

    public String toString() {
        return "Student [ name: " + name + ", age: " + age + " ]";
    }
}